﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StoreHoursDomain.Entity;
using Domain.Provider;
using WebUI.Models;
using Domain.ActiveDirectory.Provider;

namespace WebUI.Controllers
{
    public class HoursController : Controller
    {
        private readonly IDataProvider<Store> _storeRepository;
        private readonly IActiveDirectoryDataProvider _iADP;

        public HoursController(IDataProvider<Store> storeRepository,IActiveDirectoryDataProvider iADP)
        {
            _storeRepository = storeRepository;
            _iADP = iADP;
        }

        public ActionResult Index()
        {
            return this.Summary();
        }

        public ActionResult Email(int id)
        {
            return View(id);
        }

        public ActionResult SendEmail(int id, String message)
        {
            var  Group = _iADP.GetGroupsByName("" + id + " GM").FirstOrDefault();
            Domain.ActiveDirectory.Model.User user = (Domain.ActiveDirectory.Model.User) _iADP.GetUsersByUsername(Group.MemberNames.FirstOrDefault());
            //user.Email
            return RedirectToAction("Summary");
        }

        public ActionResult Summary()
        {
            var test = _storeRepository.Entities;

            return View(_storeRepository.Entities.ToList());
        }

        public ActionResult Details(int id)
        {
            return View(StoreViewModel.GetStoreViewModel(_storeRepository.Entities.ToList().FirstOrDefault(s => s.ID == id)));//  First(s => s.ID == 24));
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var bs = filterContext.ActionParameters;
            
            base.OnActionExecuting(filterContext);
        }

        public ActionResult Delete(WeeklyStoreHours wsh)
        {
            Store store = _storeRepository.Entities.ToList().FirstOrDefault(s => s.ID == wsh.storeId);
            WeeklyStoreHours storehours = store.AllHours.FirstOrDefault(s => s.weeklyID == wsh.weeklyID);
            store.AllHours.Remove(storehours);
            _storeRepository.SaveEntity(store, null);
            return RedirectToAction("Details", new { id = wsh.storeId });
        }

        public ActionResult AddDate(int id)
        {
            Store store = _storeRepository.Entities.ToList().FirstOrDefault(s => s.ID == id);
            String dateParam;
            List<DateTime> usedDates = new List<DateTime>();
            for (int i = 0; i < 6; i++)
            {
                if ((dateParam = Request.Params["effectiveDate" + i]) != "")
                {
                    DateTime date = DateTime.Parse(Request.Params["effectiveDate" + i]);

                    if (!usedDates.Contains(date))
                    {
                        getHours(store, date);
                        usedDates.Add(date);
                    }
                }
            }
            _storeRepository.SaveEntity(store, null);
            return RedirectToAction("Details", new { id = id });
        }

        public void getHours(Store store, DateTime date)
        {
            //if date time is a .what day
            WeeklyStoreHours weeklyHours = new WeeklyStoreHours();
            
            //while (date.DayOfWeek != DayOfWeek.Sunday)date = date.AddDays(-1);
            //Boolean sunday = true;
            //if(date.DayOfWeek != DayOfWeek.Sunday)sunday = false;
            DayOfWeek[] daysOfWeek = Enum.GetValues(typeof(DayOfWeek)) as DayOfWeek[];
            for (int index = 0; index < daysOfWeek.Length; index++)
            {
                var day = daysOfWeek[index];
                StoreHours hours = new StoreHours();

                string startHourKey = String.Format("start{0}Hour", day.ToString());
                string startMinuteKey = String.Format("start{0}Minute", day.ToString());
                string startMeridianKey = String.Format("start{0}Meridian", day.ToString());
                string endHourKey = String.Format("end{0}Hour", day.ToString());
                string endMinuteKey = String.Format("end{0}Minute", day.ToString());
                string endMeridianKey = String.Format("end{0}Meridian", day.ToString());
                if (Request.Params[startHourKey] == null || Request.Params[startMinuteKey] == null || Request.Params[startHourKey] == null ||
                    Request.Params[endMinuteKey] == null || Request.Params[startMeridianKey] == null || Request.Params[endMeridianKey] == null ||
                    Request.Params[startHourKey] == "-1" || Request.Params[startMinuteKey] == "-1" || Request.Params[startHourKey] == "-1" ||
                    Request.Params[endMinuteKey] == "-1" || Request.Params[startMeridianKey] == "-1" || Request.Params[endMeridianKey] == "-1")
                {
                    hours.Closed = true;
                    hours.StartTime = DateTime.MinValue;
                    hours.EndTime = DateTime.MinValue;
                    weeklyHours.SetHours(day, hours);
                }
                else
                {
                    int startHour = int.Parse(Request.Params[startHourKey]);
                    int startMinute = int.Parse(Request.Params[startMinuteKey]);
                    int endHour = int.Parse(Request.Params[startHourKey]);
                    int endMinute = int.Parse(Request.Params[endMinuteKey]);
                    string startMeridian = Request.Params[startMeridianKey];
                    string endMeridian = Request.Params[endMeridianKey];

                    int hourOffset = 0;
                    if (startMeridian == "PM") hourOffset = 12;

                    hours.StartTime = DateTime.MinValue;
                    hours.StartTime = hours.StartTime.AddHours(startHour);
                    hours.StartTime = hours.StartTime.AddMinutes(startMinute);

                    hourOffset = endMeridian == "PM" ? 12 : 0;

                    hours.EndTime = DateTime.MinValue;
                    hours.EndTime = hours.EndTime.AddHours(endHour + hourOffset);
                    hours.EndTime = hours.EndTime.AddMinutes(endMinute);
                }
                //DateTime d = new DateTime();
                //if (!sunday)
                //{
                //    if ((int)date.DayOfWeek < index) date = date.AddDays(1);
                //    if (index == 0) d = date.AddDays(7 -  (int)date.DayOfWeek);
                //    else d = date;
                //}
                //else d = date;
                weeklyHours.SetHours(day, hours);
                weeklyHours.EffectiveDate = date;
                //weeklyHours.SetDate(day, d);
            }
            //return weeklyHours;
            store.AllHours.Add(weeklyHours);
        }
    }
}
